package designMode.statePattern.after;

import java.util.Random;

/**
 * @author LIBOFEI
 * @ClassName HasQuarterState
 * @description
 * @date 2020/6/28 16:51
 * @Version 1.0
 **/
public class HasQuarterState implements State {

    GumballMachine gumballMachine;

    Random randowWinner = new Random(System.currentTimeMillis());

    public HasQuarterState(GumballMachine gumballMachine){
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("您已经投过币");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("为您退币中");
        gumballMachine.setState(gumballMachine.getNoQuarterState());
    }

    @Override
    public void turnCrank() {
        System.out.println("稍等..为您发送糖果");
        int winner = randowWinner.nextInt(10);
        if((winner < 5) && (gumballMachine.getCount() > 1)){
            gumballMachine.setState(gumballMachine.getWinnerState());
        }else{
            gumballMachine.setState(gumballMachine.getSoldState());
        }
    }

    @Override
    public void dispense() {
        System.out.println("发生错误");
    }
}
